package lyj.springcloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.lyj.springcloud.entities.CommonResult;
import com.lyj.springcloud.entities.Payment;
import lyj.springcloud.blockhandler.MyBlockHandler;
import lyj.springcloud.feign.FeignService;
import lyj.springcloud.feign.impl.FeignServiceImpl;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * OpenFeign 服务降级
 */
@RestController
public class OrderOpenFeignNacosController {

    @Resource
    FeignService feignService;

    @GetMapping("/order/feign/{id}")
    @SentinelResource(value = "openfignfallBack"
            ,blockHandler = "block_Handler_method"
            )
    public CommonResult fallBack(@PathVariable("id") String id) {
        CommonResult<Payment> result = feignService.paymentSQL(id);
//        if (id.equalsIgnoreCase("4")) {
//            throw new IllegalArgumentException ("IllegalArgumentException,非法参数异常....");
//        }else if (result.getData() == null) {
//            throw new RuntimeException ("该ID没有对应记录,空指针异常");
//        }
        return result;
    }

    /**
     * 超出规则触发sentinel控制台配置时的兜底方法
     *    兜底的方法的参数，和原来方法的参数必须一致，并且兜底的方法里面需要增加一个【BlockException exception】参数
     * @param id
     * @param e
     * @return
     */
    public CommonResult block_Handler_method(String id , BlockException e) {
        return new CommonResult("500",e.getClass().getSimpleName() + "，OrderOpenFeignNacosController  违反sentinel配置的限流规则，id : " + id);
    }

// ------------------------------------------------------------------------------------------------------------

    /**
     * 超出sentinel配置降级方法测试
     * @param id
     * @return
     */
    @GetMapping("/order/feign/blockMethod/{id}")
    @SentinelResource(value = "blockMethod"
            ,blockHandler = "fallBack_method_1"
            ,blockHandlerClass = MyBlockHandler.class
    )
    public CommonResult blockMethod(@PathVariable("id") String id) {
        CommonResult<Payment> result = feignService.paymentSQL(id);
//        if (id.equalsIgnoreCase("4")) {
//            throw new IllegalArgumentException ("IllegalArgumentException,非法参数异常....");
//        }else if (result.getData() == null) {
//            throw new RuntimeException ("该ID没有对应记录,空指针异常");
//        }
        return result;
    }

// ------------------------------------------------------------------------------------------------------------

    @GetMapping("/order/feign/fallBackMethod/{id}")
    @SentinelResource(value = "fallBackMethod"
            ,blockHandler = "fallBack_method_1"
            ,blockHandlerClass = MyBlockHandler.class
            ,fallback = "fallBack_Method"
    )
    public CommonResult fallBackMethod(@PathVariable("id") String id) {
        CommonResult<Payment> result = feignService.paymentSQL(id);
        if (id.equalsIgnoreCase("4")) {
            throw new IllegalArgumentException ("IllegalArgumentException,非法参数异常....");
        }else if (result.getData() == null) {
            throw new RuntimeException ("该ID没有对应记录,空指针异常");
        }
        return result;
    }

    /**
     * 业务异常降级兜底方法
     *
     * @param id
     * @param e
     * @return
     */
    public CommonResult fallBack_Method(@PathVariable("id") String id , Throwable e) {
        return new CommonResult("200","业务异常：" + e.getMessage());
    }

// ------------------------------------------------------------------------------------------------------------

    /**
     * feign 服务降级
     * @param id
     * @return
     */
    @GetMapping("/order/feign/feignFallBackMethod/{id}")
    @SentinelResource(value = "feignFallBackMethod"
            ,blockHandler = "fallBack_method_1"
            ,blockHandlerClass = MyBlockHandler.class
    )
    public CommonResult feignFallBackMethod(@PathVariable("id") String id) {
        return feignService.paymentSQL(id);
    }


}
